package net.ibizsys.eclipse.studio.data;

import java.util.ArrayList;
import java.util.List;

import net.ibizsys.eclipse.studio.i18n.I18n;
import net.ibizsys.eclipse.studio.util.StringKit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 系统
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizSystem extends IBizData
{
	private JSONObject data;

	private String codename;
	private String svnroot;
	private String logicname;
	private String id = "";
	private String pkgcodename;
	private String pkgpath;
	private String sfId;

	private List<IBizApp> apps = new ArrayList<IBizApp>();
	private List<IBizPub> pubs = new ArrayList<IBizPub>();
	private List<Project> projects = new ArrayList<Project>();

	public IBizSystem()
	{
	}

	public IBizSystem(JSONObject json)
	{
		init(json);
	}

	@Override
	public void init(JSONObject json)
	{
		this.data = json;
		this.id = json.getString("pssystemid");
		this.codename = json.getString("codename");
		this.svnroot = json.getString("svnroot");
		this.logicname = json.getString("logicname");
		this.sfId = json.getString("pssyssfpubid");
		JSONArray pubArray = json.getJSONArray("pssyssfpubs");

		if (pubArray != null)
		{
			for (int i = 0; i < pubArray.size(); i++)
			{
				pubs.add(new IBizPub(pubArray.getJSONObject(i)));
			}
		}

		if (pubs.size() > 0)
		{
			this.codename = pubs.get(0).getCodename();
			this.pkgcodename = pubs.get(0).getPkgcodename();
			if (!StringKit.IsBlank(pkgcodename))
			{
				this.pkgpath = pkgcodename.replaceAll("[.]", "/");
			} else
			{
				this.pkgpath = "";
			}
			this.sfId = pubs.get(0).getId();
		}

		JSONArray appArray = json.getJSONArray("pssysapps");

		if (appArray != null)
		{
			for (int i = 0; i < appArray.size(); i++)
			{
				JSONObject app = appArray.getJSONObject(i);

				apps.add(new IBizApp(app));
			}
		}

		for (IBizPub pub : pubs)
		{
			projects.addAll(pub.getProjs());
		}
	}

	public String getCodename()
	{
		return codename;
	}

	public String getSvnroot()
	{
		return svnroot;
	}

	public List<IBizApp> getApps()
	{
		return apps;
	}

	public List<Project> getProjects()
	{
		return projects;
	}

	public String getLogicname()
	{
		return logicname;
	}

	public Object getValue(String key)
	{
		if (data.containsKey(key))
		{
			return data.get(key);
		}
		return null;
	}

	public String getId()
	{
		return id;
	}

	public JSONObject getData()
	{
		return data;
	}

	public String getPkgcodename()
	{
		return pkgcodename;
	}

	public String getPkgpath()
	{
		return pkgpath;
	}

	public String getSFID()
	{
		return sfId;
	}

	@Override
	public String getText()
	{
		if (this.logicname == null || this.logicname.trim().length() == 0)
		{
			return I18n.Unknown;
		}
		return logicname+"<"+this.codename+">";
	}

	@Override
	public String getIcon()
	{
		return "icons/solution15.png";
	}
}
